package capaGrafica;

import javax.swing.JFrame;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.AbstractMap;
import java.util.HashMap;


import java.awt.Color;

import javax.swing.JPanel;

import capaModelo.Login_M;
import capaModelo.Modelo;

import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.io.Serializable;

import javax.swing.JLabel;


public class Login_V implements GUI{

	private  JFrame frame;
	private  JTextField txtLogin;
	private  JPasswordField txtPassword;
	private  JButton btn_login;
	private  JButton btn_cancel;
	private  JPanel menu;
	private  JButton btn_menu_registro;
	private  JButton btn_menu_listado;
	private  JLabel lblErrorLogin;
	
	/* Constantes */
	public static final int BTN_LOGIN = 1;
	public static final int BTN_CANCEL = 2;
	public static final int MENU_REGISTRO = 3;
	public static final int MENU_LISTADO = 4;
	
	
	/* tipos de cambios */
	
	public static final int ERROR_LOGIN = 1;
	
		
	/**
	 * Initialize the contents of the frame.
	 * @wbp.parser.entryPoint
	 */
	public void initialize(JFrame f, AbstractMap<String,Serializable> datos) {
		
		final Modelo miModelo = new Login_M();

		frame = f;//new JFrame();
		frame.setTitle("Catalogo - Login");
		frame.getContentPane().setLayout(null);
		
		txtLogin = new JTextField();
		txtLogin.addFocusListener(new FocusAdapter() {
			boolean txtLogin_ft = true;
			@Override
			public void focusGained(FocusEvent arg0) {
				if(txtLogin_ft){
					txtLogin.setText("");
					txtLogin_ft = false;
				}
			}
		});
		txtLogin.setText("login");
		txtLogin.setBounds(33, 48, 189, 29);
		frame.getContentPane().add(txtLogin);
		txtLogin.setColumns(10);
		
		txtPassword = new JPasswordField();
		txtPassword.addFocusListener(new FocusAdapter() {
			boolean txtPass_ft = true;
			@Override
			public void focusGained(FocusEvent e) {
				if(txtPass_ft){
					txtPassword.setText("");
					txtPass_ft = false;
				}
			}
		});
		txtPassword.setText("password");
		txtPassword.setBounds(33, 99, 189, 29);
		frame.getContentPane().add(txtPassword);
		txtPassword.setColumns(10);
		
		btn_login = new JButton("Login");
		btn_login.addActionListener(new ActionListener() {
			/* Boton de login pulsado */
			public void actionPerformed(ActionEvent arg0) {
				
				
				AbstractMap<String,Serializable> d = new HashMap<String, Serializable>();
				
				d.put("login", txtLogin.getText());
				d.put("pass", txtPassword.getPassword());
				
				miModelo.pressed(BTN_LOGIN,d);
			}
		});
		btn_login.setBounds(34, 165, 89, 23);
		frame.getContentPane().add(btn_login);
		
		btn_cancel = new JButton("Cancel");
		btn_cancel.addActionListener(new ActionListener() {
			/* pulsado boton de cancel */
			public void actionPerformed(ActionEvent e) {
				miModelo.pressed(BTN_CANCEL,null);
			}
		});
		btn_cancel.setBounds(133, 165, 89, 23);
		frame.getContentPane().add(btn_cancel);
		
		menu = new JPanel();
		menu.setForeground(Color.BLUE);
		menu.setBackground(Color.BLUE);
		menu.setBounds(0, 0, 10000, 23);
		frame.getContentPane().add(menu);
		menu.setLayout(null);
		
		btn_menu_registro = new JButton("Registro");
		btn_menu_registro.setBounds(0, 0, 89, 23);
		menu.add(btn_menu_registro);
		btn_menu_registro.setForeground(Color.WHITE);
		btn_menu_registro.addActionListener(new ActionListener() {
			/* pulsado boton menu registro */
			public void actionPerformed(ActionEvent arg0) {
				miModelo.pressed(MENU_REGISTRO,null);
			}
		});
		btn_menu_registro.setBackground(Color.BLUE);
		
		btn_menu_listado = new JButton("Listado");
		btn_menu_listado.addActionListener(new ActionListener() {
			/* pulsado boton menu listado */
			public void actionPerformed(ActionEvent e) {
				miModelo.pressed(MENU_LISTADO,null);
				
			}
		});
		btn_menu_listado.setForeground(Color.WHITE);
		btn_menu_listado.setBackground(Color.BLUE);
		btn_menu_listado.setBounds(87, 0, 89, 23);
		menu.add(btn_menu_listado);
		
		lblErrorLogin = new JLabel("Login o contrase\u00F1a incorrectos");
		lblErrorLogin.setVisible(false);
		lblErrorLogin.setForeground(Color.RED);
		lblErrorLogin.setBounds(33, 139, 189, 15);
		frame.getContentPane().add(lblErrorLogin);
		

	}


	@Override
	public void change(int code, AbstractMap<String, Serializable> datos) {
		
		switch(code){
		
		/* Login incorrecto */
		case(ERROR_LOGIN): 
			lblErrorLogin.setVisible(true);
			break;
			
		}
		
	}
}
